package com.acp.service.impl;

import com.acp.dao.acpo.DcsCollectDataMapper;
import com.acp.model.DcsCollectData;
import com.acp.service.DcsCollectDataService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.controller.construction
 * @Description: 采集数据管理实现类
 * @author: 张星亮
 * @date: 2019/4/5
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class DcsCollectDataServiceImpl implements DcsCollectDataService {
    @Resource
    private DcsCollectDataMapper dcsCollectDataMapper;

    /**
     * 工具方法
     * @param object
     * @param type
     * @return String 类型字符串
     */
    private String doValue(String object, String type) {
        String value;
        try {
            Map mapObject = (Map) JSON.parse(object);
            value = mapObject.get(type).toString();
        } catch (Exception ex) {
            value = "";
        }

        return value;
    }
    /**
     * 查询
     * @param date
     * @return 返回list集合
     */
    @Override
    public List<DcsCollectData> select(String date) {
        List<DcsCollectData> list = dcsCollectDataMapper.selectByGroup();
        String sTime = doValue(date, "startTime");
        String eTime = doValue(date, "endTime");
        String tableName = doValue(date, "tableName");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = null;
        Date endTime = null;
        Date endT = null;
        try {
            startTime = sdf.parse(sTime);
        } catch (ParseException e) {
            startTime = null;
        }
        try {
            endT = sdf.parse(eTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(endT);
            cal.add(Calendar.DATE, 1);
            endTime = sdf.parse(sdf.format(cal.getTime()));
        } catch (ParseException e) {
            endTime = null;
        }
        if (list.size() > 0) {
            for (int a = 0; a < list.size(); a++) {
                String code = list.get(a).getGroupCode();
                DcsCollectData dcsCollectData1 = DcsCollectData.builder().groupCode(code).status("待审核").num0("1").num1(null).num2(null).num3(null).table_name(tableName).startTime(startTime).endTime(endTime).build();
                int audit = dcsCollectDataMapper.selectCollectData(dcsCollectData1);
                list.get(a).setAuditCount(dcsCollectDataMapper.selectCollectData(dcsCollectData1));
                DcsCollectData dcsCollectData2 = DcsCollectData.builder().groupCode(code).status("待入库").num0(null).num1("1").num2(null).num3(null).table_name(tableName).startTime(startTime).endTime(endTime).build();
                int put = dcsCollectDataMapper.selectCollectData(dcsCollectData2);
                list.get(a).setPutCount(dcsCollectDataMapper.selectCollectData(dcsCollectData2));
                DcsCollectData dcsCollectData3 = DcsCollectData.builder().groupCode(code).status("已入库").num0(null).num1(null).num2("1").num3(null).table_name(tableName).startTime(startTime).endTime(endTime).build();
                int in = dcsCollectDataMapper.selectCollectData(dcsCollectData3);
                list.get(a).setInCount(dcsCollectDataMapper.selectCollectData(dcsCollectData3));

                DcsCollectData dcsCollectData5 = DcsCollectData.builder().groupCode(code).status("已入库").operation("新增").num0(null).num1(null).num2("1").num3(null).table_name(tableName).startTime(startTime).endTime(endTime).build();
                int add = dcsCollectDataMapper.selectCollectData(dcsCollectData5);
                list.get(a).setAddCount(add);

                DcsCollectData dcsCollectData6 = DcsCollectData.builder().groupCode(code).status("已入库").operation("更新").num0(null).num1(null).num2("1").num3(null).table_name(tableName).startTime(startTime).endTime(endTime).build();
                int update = dcsCollectDataMapper.selectCollectData(dcsCollectData6);
                list.get(a).setUpdateCount(update);

                DcsCollectData dcsCollectData4 = DcsCollectData.builder().groupCode(code).status("未通过审核").num0(null).num1(null).num2(null).num3("1").table_name(tableName).startTime(startTime).endTime(endTime).build();
                int reject = dcsCollectDataMapper.selectCollectData(dcsCollectData4);
                list.get(a).setRejectCount(reject);

                list.get(a).setCount(audit + put + in + reject);
            }
        }
        return list;
    }

    /**
     * 查询所有小组
     * @return 返回list集合
     */
    @Override
    public List<DcsCollectData> selectAllGroup(String date) {
        List<DcsCollectData> list = dcsCollectDataMapper.selectByGroup();
        String sTime = doValue(date, "startTime");
        String eTime = doValue(date, "endTime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = null;
        Date endTime = null;
        Date endT =null;
        try {
            startTime = sdf.parse(sTime);
        } catch (ParseException e) {
            startTime = null;
        }
        try {
            endT = sdf.parse(eTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(endT);
            cal.add(Calendar.DATE, 1);
            endTime = sdf.parse(sdf.format(cal.getTime()));
        } catch (ParseException e) {
            endTime = null;
        }
        if (list.size() > 0) {
            for (int a = 0; a < list.size(); a++) {
                String code = list.get(a).getGroupCode();
                DcsCollectData dcsCollectData1 = DcsCollectData.builder().groupCode(code).status("待审核").num0("1").num1(null).num2(null).num3(null).startTime(startTime).endTime(endTime).build();
                int audit = dcsCollectDataMapper.selectCollectData(dcsCollectData1);
                list.get(a).setAuditCount(dcsCollectDataMapper.selectCollectData(dcsCollectData1));

                DcsCollectData dcsCollectData2 = DcsCollectData.builder().groupCode(code).status("待入库").num0(null).num1("1").num2(null).num3(null).startTime(startTime).endTime(endTime).build();
                int put = dcsCollectDataMapper.selectCollectData(dcsCollectData2);
                list.get(a).setPutCount(dcsCollectDataMapper.selectCollectData(dcsCollectData2));

                DcsCollectData dcsCollectData3 = DcsCollectData.builder().groupCode(code).status("已入库").num0(null).num1(null).num2("1").num3(null).startTime(startTime).endTime(endTime).build();
                int in = dcsCollectDataMapper.selectCollectData(dcsCollectData3);
                list.get(a).setInCount(dcsCollectDataMapper.selectCollectData(dcsCollectData3));

                DcsCollectData dcsCollectData5 = DcsCollectData.builder().groupCode(code).status("已入库").operation("新增").num0(null).num1(null).num2("1").num3(null).startTime(startTime).endTime(endTime).build();
                int add = dcsCollectDataMapper.selectCollectData(dcsCollectData5);
                list.get(a).setAddCount(add);

                DcsCollectData dcsCollectData6 = DcsCollectData.builder().groupCode(code).status("已入库").operation("更新").num0(null).num1(null).num2("1").num3(null).startTime(startTime).endTime(endTime).build();
                int update = dcsCollectDataMapper.selectCollectData(dcsCollectData6);
                list.get(a).setUpdateCount(update);

                DcsCollectData dcsCollectData4 = DcsCollectData.builder().groupCode(code).status("未通过审核").num0(null).num1(null).num2(null).num3("1").startTime(startTime).endTime(endTime).build();
                int reject = dcsCollectDataMapper.selectCollectData(dcsCollectData4);
                list.get(a).setRejectCount(reject);

                list.get(a).setCount(audit + put + in + reject);
            }
        }
        return list;
    }

    /**
     * 通过编号查询小组
     * @param date
     * @return 返回list集合
     */
    @Override
    public List<DcsCollectData> selectAllGroupByCode(String date) {
        String sTime = doValue(date, "startTime");
        String eTime = doValue(date, "endTime");
        String groupCode = doValue(date, "groupCode");
        DcsCollectData dcsCollectData = DcsCollectData.builder().groupCode(groupCode).build();
        List<DcsCollectData> list = dcsCollectDataMapper.selectAllGroupByName(dcsCollectData);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = null;
        Date endTime = null;
        Date endT =null;
        try {
            startTime = sdf.parse(sTime);
        } catch (ParseException e) {
            startTime = null;
        }
        try {
            endT = sdf.parse(eTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(endT);
            cal.add(Calendar.DATE, 1);
            endTime = sdf.parse(sdf.format(cal.getTime()));
        } catch (ParseException e) {
            endTime = null;
        }
        if (list.size() > 0) {
            for (int a = 0; a < list.size(); a++) {
                DcsCollectData dcsCollectData1 = DcsCollectData.builder().table_name(list.get(a).getTable_name()).groupCode(groupCode).status("待审核").num0("1").num1(null).num2(null).num3(null).startTime(startTime).endTime(endTime).build();
                int audit = dcsCollectDataMapper.selectAllGroupByCode(dcsCollectData1);
                list.get(a).setAuditCount(audit);
                DcsCollectData dcsCollectData2 = DcsCollectData.builder().table_name(list.get(a).getTable_name()).groupCode(groupCode).status("待入库").num0(null).num1("1").num2(null).num3(null).startTime(startTime).endTime(endTime).build();
                int put = dcsCollectDataMapper.selectAllGroupByCode(dcsCollectData2);
                list.get(a).setPutCount(put);
                DcsCollectData dcsCollectData3 = DcsCollectData.builder().table_name(list.get(a).getTable_name()).groupCode(groupCode).status("已入库").num0(null).num1(null).num2("1").num3(null).startTime(startTime).endTime(endTime).build();
                int in = dcsCollectDataMapper.selectAllGroupByCode(dcsCollectData3);
                list.get(a).setInCount(in);

                DcsCollectData dcsCollectData5 = DcsCollectData.builder().table_name(list.get(a).getTable_name()).groupCode(groupCode).status("已入库").operation("新增").num0(null).num1(null).num2("1").num3(null).startTime(startTime).endTime(endTime).build();
                int add = dcsCollectDataMapper.selectAllGroupByCode(dcsCollectData5);
                list.get(a).setAddCount(add);

                DcsCollectData dcsCollectData6 = DcsCollectData.builder().table_name(list.get(a).getTable_name()).groupCode(groupCode).status("已入库").operation("更新").num0(null).num1(null).num2("1").num3(null).startTime(startTime).endTime(endTime).build();
                int update = dcsCollectDataMapper.selectAllGroupByCode(dcsCollectData6);
                list.get(a).setUpdateCount(update);

                DcsCollectData dcsCollectData4 = DcsCollectData.builder().table_name(list.get(a).getTable_name()).groupCode(groupCode).status("未通过审核").num0(null).num1(null).num2(null).num3("1").startTime(startTime).endTime(endTime).build();
                int reject = dcsCollectDataMapper.selectCollectData(dcsCollectData4);
                list.get(a).setRejectCount(reject);

                list.get(a).setCount(audit + put + in + reject);
                switch (list.get(a).getTable_name()) {
                    case "traditional_industry":
                        list.get(a).setTable_name("传统产业");
                        break;
                    case "historical_street":
                        list.get(a).setTable_name("历史文化街区");
                        break;
                    case "old_wall":
                        list.get(a).setTable_name("古城墙");
                        break;
                    case "historical_area":
                        list.get(a).setTable_name("历史地段");
                        break;
                    case "industrial_heritage":
                        list.get(a).setTable_name("工业遗产");
                        break;
                    case "old_tree":
                        list.get(a).setTable_name("古树名木");
                        break;
                    case "watercourse":
                        list.get(a).setTable_name("河道水系");
                        break;
                    case "historical_building":
                        list.get(a).setTable_name("历史建筑");
                        break;
                    case "nch":
                        list.get(a).setTable_name("非物质文化遗产");
                        break;
                    case "old_structure":
                        list.get(a).setTable_name("古构筑物");
                        break;
                    case "uar":
                        list.get(a).setTable_name("地下文物埋藏区");
                        break;
                    case "traditional_houses":
                        list.get(a).setTable_name("传统民居");
                        break;
                    case "protection_unit":
                        list.get(a).setTable_name("文控保单位");
                        break;
                    case "suzhou_gardens":
                        list.get(a).setTable_name("苏州园林");
                        break;
                    case "ucr":
                        list.get(a).setTable_name("尚未核定为文物保护单位的不可移动文物");
                        break;
                    case "wu_culture_place":
                        list.get(a).setTable_name("吴文化地名");
                        break;
                    case "hsataoc":
                        list.get(a).setTable_name("近现代重要史迹及代表性建筑");
                        break;
                    case "others":
                        list.get(a).setTable_name("其他");
                        break;
                     default:
                         break;
                }
            }
        }
        return list;
    }
}
